package com.mz.mattress.admin.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mz.mattress.admin.api.req.BlackQueryReq;
import com.mz.mattress.admin.api.req.OrderUseRecordQueryReq;
import com.mz.mattress.admin.mapper.OrderDataMapper;
import com.mz.mattress.admin.mapper.OrderUseTimesBonusMapper;
import com.mz.mattress.admin.mapper.OrderUseTimesMapper;
import com.mz.mattress.admin.service.OrderUseTimesService;
import com.mz.mattress.api.common.Result;
import com.mz.mattress.api.entity.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class OrderUseTimesServiceImpl implements OrderUseTimesService {

    @Autowired
    private OrderUseTimesMapper orderUseTimesMapper;

    @Autowired
    private OrderUseTimesBonusMapper orderUseTimesBonusMapper;

    @Autowired
    private OrderDataMapper orderDataMapper;

    @Override
    public Result<IPage<OrderUseTimes>> listPage(OrderUseRecordQueryReq req) {

        Page page = req.newPage();
        IPage<OrderUseTimes> listPage = orderUseTimesMapper.listPage(page,
                req.getOrderId(), req.getMerchantId(), req.getMerchantName(),
                req.getDeviceId(), req.getStartTime(), req.getEndTime(), req.getCity());

        List<OrderUseTimes> records = listPage.getRecords();
        if (records.isEmpty()) {
            return Result.ok(listPage);
        }

        Integer months = null;
        if (req.getStartTime() != null && req.getEndTime() != null) {
            if (req.getStartTime().equals("2025-03-01") && req.getEndTime().equals("2025-03-31")) {
                months = 3;
            } else if (req.getStartTime().equals("2025-04-01") && req.getEndTime().equals("2025-04-30")) {
                months = 4;
            } else if (req.getStartTime().equals("2025-05-01") && req.getEndTime().equals("2025-05-31")) {
                months = 5;
            } else if (req.getStartTime().equals("2025-06-01") && req.getEndTime().equals("2025-06-30")) {
                months = 6;
            }
        }

        OrderUseTimes times = records.get(0);

        if (months != null) {
            OrderData orderData = orderDataMapper.selectByMonth(months);

            SummaryTimes summaryTimes = new SummaryTimes();
            summaryTimes.setTimesCount(orderData.getTimesCount());
            summaryTimes.setUserCount(orderData.getUserCount());
            summaryTimes.setBonusMer(orderData.getBonusMer());
            summaryTimes.setBonusSys(orderData.getBonusSys());
            summaryTimes.setBonusSale(orderData.getBonusSale());
            times.setSummaryTimes(summaryTimes);

        } else {
            SummaryTimes summaryTimes = orderUseTimesMapper.listTotal(req.getOrderId(), req.getMerchantId(),
                    req.getMerchantName(), req.getDeviceId(), req.getStartTime(), req.getEndTime());
            times.setSummaryTimes(summaryTimes);
        }

        return Result.ok(listPage);
    }

    @Override
    public Result<List<OrderUseTimes>> list(Long orderId) {
        List<OrderUseTimes> list = orderUseTimesMapper.list(orderId);
        return Result.ok(list);
    }

    @Override
    public Result<OrderUseTimes> getById(Integer id) {

        OrderUseTimes record = orderUseTimesMapper.selectById(id);

        List<OrderUseTimesBonus> list = orderUseTimesBonusMapper.selectByRecordId(record.getId());
        record.setList(list);

        return Result.ok(record);
    }

    @Override
    public Result<IPage<String>> selectMobileByMerchantId(BlackQueryReq req) {
        IPage<String> list = orderUseTimesMapper.selectMobileByMerchantId(req.newPage(), req.getMerchantId());
        return Result.ok(list);
    }

}
